<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="ajax.jsp"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>"/>
    <title>${title}</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f0f0f0;
        }

        .login-block {
            max-width: 800px;
            margin: 0 auto;
            padding: 10px;
            background-color: #fff;
            border: 1px solid #ccc;
            margin-top: 20px;
        }

        #login-ur_here {
            margin-top: 10px;
            font-size: 16px;
            color: #333;
        }

        .login-box {
            padding: 10px;
            background-color: #f9f9f9;
            border: 1px solid #ddd;
        }

        .login-box_1 {
            padding: 10px;
            background-color: #fff;
            border: 1px solid #ddd;
        }

        table {
            width: 100%;
        }

        input[type="text"],
        input[type="password"] {
            padding: 5px;
            margin: 5px 0;
            width: 100%;
            height: 25px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        .login-bnt_blue_1 {
            padding: 5px;
            margin: 5px 0;
            width: 100%;
            height: 25px;
            border: 1px solid #ccc;
            border-radius: 3px;
            background-color: #007bff;
            color: #fff;
            height: 35px;
            cursor: pointer;
        }

        .login-bnt_blue_1:hover {
            background-color: #0056b3;
        }
    </style>
</head>

<body>
<jsp:include page="header.jsp"></jsp:include>
<div class="login-block login-box">
    <div class="blank"></div>
    <div id="login-ur_here">
        当前位置: <a href="/user/index">首页</a>
        <code> &gt; </code>
        用户登录
    </div>
</div>
<script>
    window.onload = function (){
        var urlParams = new URLSearchParams(window.location.search);
        var paramValue = urlParams.get('error');
        if (paramValue !== null) {
            // 参数存在，获取到值
            console.log('Value of "error":', paramValue);
            document.getElementById('msg').innerHTML = paramValue;
            setTimeout(()=>{
                document.getElementById('msg').innerHTML = "";
            }, 2000);
        }
    }

    function userLogin(){
        let username = document.getElementById('username').value;
        let password = document.getElementById('password').value;
        if (username === '' || password === ''){
            document.getElementById('msg').innerHTML = '请填写用户名和密码';
            setTimeout(()=>{
                document.getElementById('msg').innerHTML = '';
            }, 2000);
            return;
        }
        let loginUser = {
            username: username,
            password: password,
        };
        console.log("loginUser==>", loginUser);
        axiosInstance.post(
            '/user/login',
            loginUser,
        ).then((res)=>{
            console.log(res.data.data);
            if (res.data.code == 200){
                localStorage.setItem('token', res.data.data.token);
                localStorage.setItem('userInfo', JSON.stringify(res.data.data));
                localStorage.setItem('role', 'user');
                navigateTo('/user/index');
            }else {
                console.log("登录失败");
                document.getElementById('msg').innerHTML = res.data.desc;
                setTimeout(()=>{
                    document.getElementById('msg').innerHTML = "";
                }, 2000);
            }
        }).catch((e)=>{
            console.log(e);
            console.log("登录出现异常");
            document.getElementById('msg').innerHTML = "登录出现异常";
            setTimeout(()=>{
                document.getElementById('msg').innerHTML = "";
            }, 2000);
        })
    }
</script>
<div class="blank"></div>
<div id="msg"></div>
<div class="login-block">
    <div class="login-box">
        <div class="error"></div>
        <div class="login-box_1">
            <h3>
                <span>用户登录</span>
            </h3>
            <table border="0" cellpadding="0" cellspacing="0">
                <tr valign="top">
                    <td bgcolor="#FFFFFF" align="center">
                        <form name="myform">
                            <table width="60%" border="0" cellpadding="5" cellspacing="1" bgcolor="#dddddd">
                                <tr>
                                    <td width="28%" align="right" bgcolor="#FFFFFF">用户名：</td>
                                    <td width="76%" align="left" bgcolor="#FFFFFF"><input type="text"
                                                    name="username" size="25" class="login-inputBg"
                                                    id="username" required/></td>
                                </tr>
                                <tr>
                                    <td width="28%" align="right" bgcolor="#FFFFFF">密码：</td>
                                    <td width="76%" align="left" bgcolor="#FFFFFF">
                                        <input name="password" type="password" size="25"
                                               id="password" class="login-inputBg" required /></td>
                                </tr>
                                <tr>
                                    <td colspan="1"></td>
                                    <td colspan="1" align="center" bgcolor="#FFFFFF">
                                        <input class="login-bnt_blue_1" type="button" onclick="userLogin()" value="确认登录"/>
                                    </td>
                                </tr>
                            </table>
                        </form>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div class="blank5"></div>
</div>
<jsp:include page="footer.jsp"></jsp:include>
</body>
</html>
